using System.Net.Http;
using Microsoft.Owin;
using Microsoft.Owin.Logging;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.DataHandler;
using Microsoft.Owin.Security.DataProtection;
using Microsoft.Owin.Security.Infrastructure;
using Owin;

namespace Sharp.Portal.Mvc.OAuth.WeiBo
{
	public class AuthenticationMiddleware : AuthenticationMiddleware<WeiBoAuthenticationOptions>
	{
		private readonly ILogger _logger;

		private readonly HttpClient _httpClient;

		public AuthenticationMiddleware(OwinMiddleware next, IAppBuilder app, WeiBoAuthenticationOptions options) : base(next, options)
		{
			if (string.IsNullOrEmpty(base.Options.SignInAsAuthenticationType))
			{
				options.SignInAsAuthenticationType = app.GetDefaultSignInAsAuthenticationType();
			}
			if (options.StateDataFormat == null)
			{
				IDataProtector dataProtector = app.CreateDataProtector(new string[]
				{
					typeof(AuthenticationMiddleware).FullName,
					options.AuthenticationType
				});
				options.StateDataFormat = new PropertiesDataFormat(dataProtector);
			}
			this._logger = app.CreateLogger<AuthenticationMiddleware>();
			this._httpClient = new HttpClient();
			this._httpClient.MaxResponseContentBufferSize = 10485760L;
		}

		protected override AuthenticationHandler<WeiBoAuthenticationOptions> CreateHandler()
		{
			return new AuthenticationHandler(this._httpClient, this._logger);
		}
	}
}
